extends Node2D

@onready var spawn_position: Node2D = $SpawnPosition
@onready var enemy_box: Node2D = $EnemyBox

signal enemy_clear

## 轻型坦克
@export var tank_light_num := 0
## 中型坦克
@export var tank_medium_num := 0
## 重型坦克
@export var tank_heavy_num := 0
## buff掉落概率
@export var buff_probability := 0.0

var enemy_count := 0


var enemy_index: Array[PackedScene] = [
	preload("res://entity/enemy/enemy_tank_light.tscn"),
	preload("res://entity/enemy/enemy_tank_medium.tscn"),
	preload("res://entity/enemy/enemy_tank_heavy.tscn"),
]

var buff_type_arr : Array = [
	preload("res://entity/buff/boat_buff.tscn"),
	preload("res://entity/buff/damage_buff.tscn"),
	preload("res://entity/buff/health_buff.tscn"),
	preload("res://entity/buff/health_max_buff.tscn"),
	preload("res://entity/buff/fire_rate_buff.tscn"),
	#preload("res://entity/buff/rotation_buff.tscn"),
	preload("res://entity/buff/shell_speed_buff.tscn"),
	preload("res://entity/buff/speed_buff.tscn"),
	#preload("res://entity/buff/turret_rotation_buff.tscn"),
]

@onready var enemy_num: Array = [
	tank_light_num,
	tank_medium_num,
	tank_heavy_num,
]


func _ready() -> void:
	var pos_num := spawn_position.get_children().size()
	var enemy_total_num : int = VarHelper.array_sum(enemy_num)
	var max_pos_num := pos_num if enemy_total_num >= pos_num else enemy_total_num
	for pos_index in range(max_pos_num):
		var spawn_pos := spawn_position.get_child(pos_index)
		spawn_pos.show()
		spawn_pos.get_node("SpawnTimer").timeout.connect(_on_spawn_timer_timeout.bind(pos_index))
		spawn_pos.get_node("SpawnTimer").start()
		spawn_pos.get_node("HideTimer").start()


func _on_spawn_timer_timeout(pos_i: int = -1) -> void:
	if multiplayer.is_server():
		var can_spawn_index := []
		for i in range(enemy_num.size()):
			if enemy_num[i] > 0:
				can_spawn_index.append(i)
		if can_spawn_index.size():
			var index := randi_range(0, can_spawn_index.size() - 1)
			enemy_num[index] -= 1
			var pos_index := pos_i if pos_i > -1 else randi_range(0, spawn_position.get_child_count() - 1)
			var has_buff := randf_range(0, 100) < buff_probability
			var buff_type := randi_range(0, buff_type_arr.size() - 1) if has_buff else -1
			spawn.rpc(index, pos_index, buff_type)


@rpc("any_peer", "call_local", "reliable")
func spawn(index: int, pos_index: int, buff_type: int) -> void:
	var pos: Vector2 = spawn_position.get_child(pos_index).global_position
	var ob := enemy_index[index].instantiate()
	ob.name = str("enemy_" + str(enemy_count))
	ob.buff_type = buff_type;
	ob.global_position = pos
	ob.tree_exited.connect(enemy_die.bind(index, pos_index, ob))
	enemy_box.add_child(ob)
	enemy_count += 1


func enemy_die(_index: int, pos_index: int, ob: Node2D) -> void:
	if not get_tree(): return
	
	if ob.buff_type > -1:
		Global.add_scene(buff_type_arr[ob.buff_type], ob.global_position, Vector2(0.25, 0.25), 0.0, get_node("/root/Main/World/Objects/Buff"))
	if VarHelper.array_sum(enemy_num) == 0: 
		enemy_clear.emit()
		return
	
	var spawn_pos : Node2D = spawn_position.get_child(pos_index)
	spawn_pos.show()
	spawn_pos.get_node("SpawnTimer").start()
	spawn_pos.get_node("HideTimer").start()



